/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package iteracon.stack;

import iteracon.IteraconBase;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import iteracon.stack.iterator.Pulley;
import iteracon.stack.iterator.StkIter;
import iteracon.tuple.Tuple;

/**
 * stack base abstract class
 * @author martin
 */
public abstract class StkBase<T> extends IteraconBase<T> implements Stk<T>{
  @Override
  public ListIterator<T> listIterator() {
    return Pulley.fromStack(this);
  }
  
  @Override
  public ListIterator<T> listIterator(int index) {
    return Pulley.fromStackAtIndex(this, index);
  }

  @Override
  public List<T> subList(int fromIndex, int toIndex) {
    //catastrophic on stack
    return (List<T>)Tuple.make(toArray()).subList(fromIndex, toIndex);
  }
  
  @Override
  public T get(int index) {
//    for (T t : this)
//      if (index > 0)
//        index--;
//      else
//        return t;
//    throw new IndexOutOfBoundsException();
    throw new UnsupportedOperationException("indexing linkedlist is catastrophic");
  }
  
  //iterable
  @Override
  public Iterator<T> iterator() {
    return new StkIter(this);
  }
  

  @Override
  public Stk<T> push(final T t) {
    return PushStk.addTo(this, t);
  }
}
